package com.cloudwise.util;

import com.cloudwise.entity.LayUiTree;
import com.cloudwise.entity.Menu;

import java.util.ArrayList;
import java.util.List;

public class TreeUtil {
    /**
     * @create by: wang
     * @description:  通过menulist返回treeList
     * @create time: 2022/1/18 15:15:36
     * @param menuList
     * @param parentId
     * @return java.util.List<com.cloudwise.entity.LayUiTree>
     */
    public static List<LayUiTree> getTreeListfromMenuList(List<Menu> menuList,Integer parentId){
        List<LayUiTree> layUiTreeList= new ArrayList<>();
        for (Menu menu : menuList) {
            //判断传入的parentId是否有孩子
            if(menu.getParentId().equals(parentId)){
                LayUiTree tree = getLayUiTreeFromMenu(menu);
                //找孩子，递归调用
                tree = recursion(tree, menuList);
                layUiTreeList.add(tree);
            }
        }
        return layUiTreeList;
    }
    /**
     * @create by: wang
     * @description: 递归调用，匹配孩子
     * @create time: 2022/1/18 15:15:36
     * @param
     * @return com.cloudwise.entity.LayUiTree
     */
    public static LayUiTree recursion(LayUiTree tree,List<Menu> menuList){
        //定义一个集合，装孩子用的
        List<LayUiTree> childNodes=new ArrayList<>();
        for (Menu node : menuList) {
            if(node.getParentId().equals(tree.getId())){
                LayUiTree treeNode = getLayUiTreeFromMenu(node);
                //递归调用
                childNodes.add(recursion(treeNode,menuList));
            }
        }
        tree.setChildren(childNodes);

        return tree;
    }
    /**
     * @create by: wang
     * @description:传入menu返回layuitree
     * @create time: 2022/1/18 15:15:36
     * @param menu
     * @return com.cloudwise.entity.LayUiTree
     */
    public static   LayUiTree getLayUiTreeFromMenu( Menu menu){
        LayUiTree tree= new LayUiTree();
        tree.setId(menu.getMenuId());
        tree.setTitle(menu.getMenuName());
        tree.setUrl(menu.getUrl());
        tree.setIcon(menu.getIcon());
        return tree;
    }
}
